package com.tengfei.blogsystem.service.impl;


import com.tengfei.blogsystem.dao.LinkDao;
import com.tengfei.blogsystem.pojo.FriendsLink;
import com.tengfei.blogsystem.response.ResponseResult;
import com.tengfei.blogsystem.response.ResponseState;
import com.tengfei.blogsystem.service.ILinkService;
import com.tengfei.blogsystem.utils.Constants;
import com.tengfei.blogsystem.utils.SnowFlakeIdWorker;
import com.tengfei.blogsystem.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-21-19:34
 * @description
 */
@Service
@Transactional
public class LinkServiceImpl implements ILinkService {

    @Autowired
    private LinkDao linkDao;
    @Autowired
    private SnowFlakeIdWorker idWorker;

    @Override
    public ResponseResult listLink(int page, int size) {
        // 分页查询
        if (page < Constants.PageValue.DEFAULT_PAGE) {
            page = Constants.PageValue.DEFAULT_PAGE;
        }
        if (size < Constants.PageValue.MIN_SIZE) {
            size = Constants.PageValue.MIN_SIZE;
        }
        // 排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<FriendsLink> categories = linkDao.findAll(pageable);
        return new ResponseResult(ResponseState.SUCCESS, "查询友链列表成功", categories);
    }

    @Override
    public ResponseResult uploadLink(FriendsLink link) {
        // 检查数据
        if (TextUtils.isEmpty(link.getName())) {
            return new ResponseResult(ResponseState.ERROR, "友链名称不可以为空");
        }
        if (TextUtils.isEmpty(link.getLogo())) {
            return new ResponseResult(ResponseState.ERROR, "友链logo不可以为空");
        }
        if (TextUtils.isEmpty(link.getUrl())) {
            return new ResponseResult(ResponseState.ERROR, "友链链接不可以为空");
        }
        // 补全数据
        link.setId(String.valueOf(idWorker.nextId()));
        link.setState("1");
        link.setCreateTime(new Date());
        link.setUpdateTime(new Date());
        // 保存数据
        linkDao.save(link);
        return new ResponseResult(ResponseState.SUCCESS, "添加友链成功");
    }

    @Override
    public ResponseResult deleteLinkByUpdateState(String linkId) {
        int result = linkDao.deleteLinkByUpdateState(new Date(), linkId);
        if (result > 0) {
            return new ResponseResult(ResponseState.SUCCESS, "删除友链成功");
        }
        return new ResponseResult(ResponseState.ERROR, "友链不存在");
    }

    @Override
    public ResponseResult updateLink(String linkId, FriendsLink link) {
        // 根据id查找
        FriendsLink linkById = linkDao.findOneById(linkId);
        if (linkById == null) {
            return new ResponseResult(ResponseState.ERROR, "友链不存在");
        }
        // 检查数据
        if (!TextUtils.isEmpty(link.getName())) {
            linkById.setName(link.getName());
        }
        if (!TextUtils.isEmpty(link.getLogo())) {
            linkById.setLogo(link.getLogo());
        }
        if (!TextUtils.isEmpty(link.getUrl())) {
            linkById.setUrl(link.getUrl());
        }
        if (link.getOrder() != 0) {
            linkById.setOrder(link.getOrder());
        }
        link.setUpdateTime(new Date());
        // 保存结果
        linkDao.save(linkById);
        return new ResponseResult(ResponseState.SUCCESS, "更新友链成功");
    }

    @Override
    public ResponseResult getLink(String linkId) {
        FriendsLink linkById = linkDao.findOneById(linkId);
        if (linkById == null) {
            return new ResponseResult(ResponseState.ERROR, "友链不存在");
        }
        return new ResponseResult(ResponseState.SUCCESS, "查询友链成功", linkById);
    }
}
